<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04 登录</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.min.js"></script>
    <style>
    </style>
</head>

<body>
    <div id="app">
        <p>
            <label for="">用户名</label>
            <input type="text" v-model="username">
        </p>
        <p>
            <label for="">密码&emsp;</label>
            <input type="text" v-model="userpwd">
        </p>
        <p>
            <label for="">是否记住用户名和密码</label>
            <input type="checkbox" :checked="isremeber" @change="getremeber">
        </p>
        <p>
            <button @click="login">登录</button>
        </p>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: "",
            userpwd: "",
            isremeber: false,


        },
        created() {
            this.username = window.localStorage.getItem("loginname");
            this.userpwd = window.localStorage.getItem("loginpwd");

        },
        methods: {

            // 获取是否记住用户名
            getremeber() {
                this.isremeber = window.event.target.checked;
            },

            async login() {

                // 判断用户
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfos",
                    params: {
                        username: this.username,
                        usrepwd: this.usrepwd,
                    },
                });

                if (list.length) {
                    alert("登录成功");

                    // 判断是否存储用户名和密码
                    if (this.isremeber) {

                        window.localStorage.setItem("loginname", this.username)
                        window.localStorage.setItem("loginpwd", this.userpwd)
                    } else {
                        // 没有勾选就清除
                        window.localStorage.removeItem("loginname")
                        window.localStorage.removeItem("loginpwd")

                    }

                    // 以对象格式存储
                    let userinfo = list[0];
                    window.localStorage.setItem("userinfo", JSON.stringify(userinfo));

                    // 跳转
                    // 还要判断是否从别的页面跳过来的
                    let url = this.urlParse();
                    console.log(url)


                    if (url.returnUrl) { // 如果存在returnUrl => 从其他页面跳转过来
                        location.href = decodeURIComponent(url.returnUrl);
                    } else {
                        location.href = "./10 list_分页_点击.html";
                    }

                } else {
                    alert("用户名或者密码不正确");
                }


            },
            urlParse() {         //url解析封装
                var search = location.search;
                var data = {};
                if (search) {
                    // console.log(search);
                    var str = search.substr(1);
                    var list = str.split("&");
                    // console.log(list);
                    for (var i = 0; i < list.length; i++) {
                        var item = list[i];
                        var key = item.split("=")[0];
                        var val = item.split("=")[1];
                        data[key] = val;
                    }
                    // console.log(data);
                }
                return data;
            },


        },

    })



</script>

</html>